<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/7/17
 * Time: 13:46
 */
namespace app\api\controller;
use think\Db;
class Answer extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        header("Access-Control-Allow-Origin:*");
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 2111){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }
    }
    //悬赏问答
    public function index(){
        $res = Db::table('up_answer')
            ->where('is_publish',1)
            ->order('answer_time desc')
            ->limit(4)
            ->select();
        foreach ($res as $key=>&$vo){
            $info = Db::table('up_answer_reply')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.reply_answer_user_id','left')
                ->field('uuu.user_username,a.reply_answer_content,a.is_show_user')
                ->order('a.reply_answer_time desc')
                ->where('a.f_id',$vo['answer_id'])
                ->find();
            $vo['info'] = $info;
            //多少个回答
            $num = Db::table('up_answer_reply')->alias('a')
                ->where('a.f_id',$vo['answer_id'])
                ->join('up_u_user uuu','uuu.user_id = a.reply_answer_user_id','left')
                ->count();
            $vo['num'] = $num;
        }
        return  json(array('status'=>200,'msg'=>$res));
    }
    //举报
    public function report(){
        extract(input());
        $r_type_id = input('param.r_type_id');
        $data['r_user_id'] = $user_id;
        $data['report_user_id'] = $report_user_id;
        $data['report_time'] = date('Y-m-d H:i:s',time());
        $data['report_type'] = 3;
        $data['r_type_id'] = $r_type_id;
        $data['report_intro'] = input('param.report_intro');
        $res = Db::table('up_report')->insertGetId($data);
        if($res){
            return  json(array('status'=>200,'msg'=>"成功"));
        }else{
            return  json(array('status'=>100,'msg'=>"失败"));
        }
    }
    //删除问题
    public function del(){
        extract(input());
        $id = input('param.id');
        $info = Db::table('up_answer')->where('answer_id',$id)->find();
        if($info['answer_user_id']!=$user_id){
            return  json(array('status'=>100,'msg'=>"不能删除不属于自己的悬赏问答"));
        }else{
            $info1 = Db::table('up_answer_reply')->where('f_id',$id)->count();
            if($info1>=1){
                 Db::table('up_answer_reply')->where('f_id',$id)->delete();
            }
            $res = Db::table('up_answer')->where('answer_id',$id)->delete();
            if($res){
                return  json(array('status'=>200,'msg'=>"删除成功"));
            }else{
                return  json(array('status'=>100,'msg'=>"删除失败"));
            }
        }
    }
    //全部悬赏问答
    public function allAnswer(){
        $page = input('param.page')?input('param.page'):1;
        $num1= 10;
        $limit = $page-1;
        $res = Db::table('up_answer')
            ->where('is_publish', 1)
            ->order('answer_time desc')
            ->limit($limit*$num1,$num1)
            ->select();
        foreach ($res as $key=>&$vo){
            $info = Db::table('up_answer_reply')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.reply_answer_user_id','left')
                ->field('uuu.user_username,a.*')
                ->where('a.f_id',$vo['answer_id'])
                ->order('a.reply_answer_time desc')
                ->find();
            $vo['info'] = $info;
            //多少个回答
            $num = Db::table('up_answer_reply')->alias('a')
                ->where('a.f_id',$vo['answer_id'])
                ->join('up_u_user uuu','uuu.user_id = a.reply_answer_user_id','left')
                ->count();
            $vo['num'] = $num;
        }
        return  json(array('status'=>200,'msg'=>$res));
    }
    //提问
    public function question(){
        extract(input());
        $title = input('param.title');
        $is_show_user = input('is_show_user'); // 是否匿名   1 不匿名   2 匿名
        $is_publish = input('is_publish'); // 是否发布   1 发布   2 草稿箱
        $answer_describe = input('param.answer_describe');
        $title = $this->sensitive($title);
        $answer_describe = $this->sensitive($answer_describe);
        $img = input('param.img');
        $video = input('param.video');
//        $answer_money= input('param.answer_money');
        $data['answer_title'] = $title;
        $data['answer_user_id'] = $user_id;
        $data['answer_time'] = date('Y-m-d H:i:s',time());
        $data['answer_describe'] = $answer_describe;
        $data['answer_img'] = $img;
        $data['answer_video'] = $video;
        $data['is_show_user'] = $is_show_user;
        $data['is_publish'] = $is_publish;
        if($is_publish == 1){
            $data['answer_money'] = input('answer_money');
        }
    
        $res = Db::table('up_answer')->insertGetId($data);
        return  json(array('status'=>200,'msg'=>$res));
    }

    //详情
    public function detail(){
        extract(input());
        $id = input('param.id');
        $res = Db::table('up_answer')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.answer_user_id','left')
            ->field('a.*,uuu.user_username')
            ->where('answer_id',$id)->find();
        if(!$res){
            return json(['status' => 0, 'msg' => '数据不存在']);
        }
        $info1= Db::table('up_answer_reply')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.reply_answer_user_id','left')
            ->field('a.*,uuu.user_username,uuu.user_image')
            ->where('f_id',$id)->order('reply_answer_time desc')->find();
        $res['info1'] = $info1;
        //多少评论
        $num = Db::table('up_answer_reply')->where('f_id',$id)->count();
        $res['num'] = $num;
        //多少点赞
        $where['give_type'] = 5;
        $where['give_f_id'] = $info1['reply_answer_id'];
        $num1 = Db::table('up_comment_give')->where($where)->count();
        $res['dz_num'] = $num1;
        //自己是否点赞
        $where1['give_type'] = 5;
        $where1['give_f_id'] = $info1['reply_answer_id'];
        $where1['give_user_id'] = $user_id;
        $info = Db::table('up_comment_give')->where($where1)->find();
        if($info['give_id']!=''){//点赞
         $res['is_dz'] = 1;
        }else{//没有点赞
          $res['is_dz'] = 2;
        }
        return  json(array('status'=>200,'msg'=>$res));
    }

    //评论点赞
    public function Dz(){
        // type  1评论回复点赞   2评论回复点赞
        extract(input());
        $type = input('param.type');
        if($type==1) {
            $data['give_user_id'] = $user_id;
            $data['give_time'] = date('Y-m-d H:i:s', time());
            $data['give_type'] = 4;
            $data['give_f_id'] = input('param.id');
            $res = Db::table('up_comment_give')->insertGetId($data);
            if ($res) {
                return json(array('status' => 200, 'msg' => "点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "点赞失败"));
            }
        }elseif ($type==2){
            $data['give_user_id'] = $user_id;
            $data['give_type'] =4;
            $data['give_f_id'] = input('param.id');
            $data['give_by_user_id'] = input('param.other_user');
            $res = Db::table('up_comment_give')->where($data)->delete();
            if ($res) {
                return json(array('status' => 200, 'msg' => "取消点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "取消点赞失败"));
            }
        }
    }

    //评论回复
    public function answer(){
        extract(input());
        $is_show_user = input('is_show_user'); // 是否匿名   1 不匿名   2 匿名
        $content = input('param.content');
        $content = $this->sensitive($content);
        $video_id = input('param.id');
        $img= input('param.img');
        $video = input('param.video');
        $data['f_id'] = $video_id;
        $data['reply_answer_content'] = $content;
        $data['reply_answer_video'] = $video;
        $data['reply_answer_img'] = $img;
        $data['reply_answer_time'] = date('Y-m-d H:i:s',time());
        $data['reply_answer_user_id'] = $user_id;
        $data['is_show_user'] = $is_show_user;
        $res = Db::table('up_answer_reply')->insertGetId($data);

        $data1['comment'] = $content;
        $data1['user_id'] = $user_id;
        $data1['time'] = date('Y-m-d H:i:s',time());
        $data1['type'] = 6;
        $data1['f_id'] = $video_id;//todo
        $data1['f_user_id'] = input('param.other_user');
        Db::table('up_comment')->insertGetId($data1);
        if($res){
            return  json(array('status'=>200,'msg'=>"发布成功"));
        }else{
            return  json(array('status'=>200,'msg'=>"发布失败"));
        }
    }

    //全部回答
    public function allQuestion(){
        extract(input());
        $id = input('param.id');
        $page = input('param.page')?input('param.page'):1;
        $num= 10;
        $limit = $page-1;
        $res = Db::table('up_answer')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.answer_user_id','left')
            ->where('a.answer_id',$id)
            ->field('a.*,uuu.user_username,uuu.user_image')
            ->find();
        //查询是否收藏这个资讯
        $whereMap4['collection_f_id'] = $id;
        $whereMap4['collection_type'] = 8;
        $whereMap4['collection_user_id'] = $user_id;
        $collection = Db::table('up_collection')->where($whereMap4)->find();
        if($collection['collection_id']!=''){
            $res['is_collection'] = 1;
        }else{
            $res['is_collection'] = 2;
        }
        //多少评论
        $count= Db::table('up_answer_reply')->where('f_id',$id)->count();
        $res['num'] = $count;
        $map2['a.f_id']  = $id;
        $map2['is_optimum'] = 2;
        $info = Db::table('up_answer_reply')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.reply_answer_user_id','left')
                ->where($map2)
                ->limit($num*$limit,$num)
                ->field('a.*,uuu.user_username,uuu.user_image')
                ->select();
        //最佳答案
        $map3['f_id']  = $id;
        $map3['is_optimum'] = 1;
        $info2 = Db::table('up_answer_reply')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.reply_answer_user_id','left')
            ->where($map3)
            ->field('a.*,uuu.user_username,uuu.user_image')
            ->find();
        if($info2){
            $map['give_f_id'] = $info2['reply_answer_id'];
            $map['give_type'] = 5;
            $count3 = Db::table('up_comment_give')->where($map)->count();
            $info2['count1'] = $count3;
            $map4['give_type'] = 5;
            $map4['give_f_id'] = $info2['reply_answer_id'];
            $map4['give_user_id'] = $user_id;
            $info4 = Db::table('up_comment_give')->where($map4)->find();
            if($info4['give_id']!=''){//点赞过
                $info2['is_dz'] = 1;
            }else{
                $info2['is_dz'] = 2;
            }
        }

        foreach ($info as $key=>&$vo){
            //评论有多少点赞
            $map['give_f_id'] = $vo['reply_answer_id'];
            $map['give_type'] = 5;
            $count1 = Db::table('up_comment_give')->where($map)->count();
            $vo['count1'] = $count1;

            $map1['give_type'] = 5;
            $map1['give_f_id'] = $vo['reply_answer_id'];
            $map1['give_user_id'] = $user_id;
            $info1 = Db::table('up_comment_give')->where($map1)->find();
            if($info1['give_id']!=''){//点赞过
                $vo['is_dz'] = 1;
            }else{
                $vo['is_dz'] = 2;
            }
        }
        $where1['is_optimum'] =1;
        $where1['f_id'] =$id;

        $is_optimum = Db::table('up_answer_reply')->where($where1)->find();
        if($is_optimum['reply_answer_id']!=''){
            $res['is_optimum'] = 1;//已经设置最佳答案
        }else{
            $res['is_optimum'] = 2; //没有
        }

        $res['info'] = $info;
        $res['info1'] = $info2;
        return  json(array('status'=>200,'msg'=>$res));
    }
    //收藏
    public function collection(){
        // type  1评论回复点赞   2评论回复点赞
        extract(input());
        $id = input('param.id');
        $data['collection_f_id'] = $id;
        $data['collection_user_id'] = $user_id;
        $data['collection_type'] = 8;
        $res1 = Db::table('up_collection')->where($data)->find();
        if($res1['collection_id']!=''){
            $res= Db::table('up_collection')->where($data)->delete();
        }else{
            $data['collection_type']=8;
            $res= Db::table('up_collection')->insertGetId($data);
        }
        if ($res) {
            return json(array('status' => 200, 'msg' => "收藏成功"));
        } else {
            return json(array('status' => 200, 'msg' => "收藏失败"));
        }
    }
    //悬赏问答回复点赞
    public function answerDz(){
        extract(input());
        $id = input('param.id');
        $where['give_type'] = 5;
        $where['give_user_id'] = $user_id;
        $where['give_f_id'] = $id;
        $res = Db::table('up_comment_give')->where($where)->find();
        if($res['give_id']!=''){//删除
            $data['give_user_id'] = $user_id;
            $data['give_type'] =5;
            $data['give_f_id'] = $id;
            $res = Db::table('up_comment_give')->where($data)->delete();
            if ($res) {
                return json(array('status' => 200, 'msg' => "取消点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "取消点赞失败"));
            }
        }else{
            //todo 后加发布用户
            $data['give_user_id'] = $user_id;
            $data['give_time'] = date('Y-m-d H:i:s', time());
            $data['give_type'] = 5;
            $data['give_f_id'] = $id;
            $data['give_f_f_id'] = input('pl_id');
            $data['give_by_user_id'] = input('param.other_user');
            $res = Db::table('up_comment_give')->insertGetId($data);
            if ($res) {
                return json(array('status' => 200, 'msg' => "点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "点赞失败"));
            }
        }
    }
    //悬赏问答回复举报
    public function reportQuestion(){
        extract(input());
        $r_type_id = input('param.id');
        $data['r_user_id'] = $user_id;
        $data['report_user_id'] = $report_user_id;
        $data['report_time'] = date('Y-m-d H:i:s',time());
        $data['report_type'] = 4;
        $data['r_type_id'] = $r_type_id;
        $data['report_intro'] = input('param.report_intro');
        $res = Db::table('up_report')->insertGetId($data);
        if($res){
            return  json(array('status'=>200,'msg'=>"成功"));
        }else{
            return  json(array('status'=>100,'msg'=>"失败"));
        }
    }
    //设置最佳答案
    public function optimum(){
        extract(input());
        $id = input('param.id');
        Db::startTrans();
        try{
            // 设置为最佳
            Db::table('up_answer_reply')->where('reply_answer_id',$id)->setField('is_optimum',1);
            // 最佳答案的用户增加余额
            $info = Db::table('up_answer_reply')->where('reply_answer_id',$id)->find();
            $info1 =  Db::table('up_answer')->where('answer_id',$info['f_id'])->find();
            Db::table('up_u_user')->where('user_id',$info['reply_answer_user_id'])->setInc('user_balance',$info1['answer_money']);
            // 插入order表
            $data = [
                'order_cate' => 3,
                'order_type' => -1,
                'order_num' => date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT),
                'order_money' => $info1['answer_money'],
                'order_status' => 2,
                'order_user_id' => $info['reply_answer_user_id'],
                'order_time' => date('Y-m-d H:i:s'),
                'order_pay_time' => date('Y-m-d H:i:s')
            ];
            Db::table('up_order')->insert($data);
            Db::commit();
            return  json(array('status'=>200,'msg'=>"成功"));
        }catch(\Exception $e){
            Db::rollback();
            return  json(array('status'=>100,'msg'=>"失败"));
    
        }
    }

    //删除答案
    public function delQuestion(){
        extract(input());
        $id = input('param.id');
        $info = Db::table('up_answer_reply')->where('reply_answer_id',$id)->find();
        if($info['reply_answer_user_id']!=$user_id){
            return  json(array('status'=>100,'msg'=>"不能删除别人的回复"));
        }else{
            $res = Db::table('up_answer_reply')->where('reply_answer_id',$id)->delete();
            if($res){
                return  json(array('status'=>200,'msg'=>"成功"));
            }else{
                return  json(array('status'=>100,'msg'=>"失败"));
            }
        }
    }
}